package cn.tianyigps.tianyigpsactivetask.assign;

import cn.tianyigps.tianyigpsactivetask.Dto.ActiveStatusDto;
import cn.tianyigps.tianyigpsactivetask.dao.EQCheckModelDao;
import cn.tianyigps.tianyigpsactivetask.model.EquipmentStatsModel;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.manger.TaskManger;
import cn.tianyigps.tianyigpspublic.bean.EquipmentModel;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: WEI.YUAN
 * @Descirption:
 * @Date: 2019/4/8_19:01
 */
@Component
@Slf4j
public class ActiveStatus {
    @Autowired
    private EQCheckModelDao eqCheckModelDao;

    @Autowired
    TaskManger taskManger;
    @Value("${data.url}")
    private String dataUrl;

    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    public void statusTask(Task task) {
        //获取task中得equipmentId
        List<String> equipmentIds = new ArrayList<>();

        ObjectMapper objectMapper = new ObjectMapper();

        //查询equipmentStatsModel条件集合
        List<ActiveStatusDto> activeStatusDtos = new ArrayList<>();

        List<EquipmentModel> equipmentCheckModels = new ArrayList<>();
        try {
            //序列化得到equipments的集合
            String json = objectMapper.writeValueAsString(task.getParams().get("equipmentIds"));
            JavaType javaType = getCollectionType(ArrayList.class, String.class);
            equipmentIds = objectMapper.readValue(json, javaType);

            equipmentCheckModels = eqCheckModelDao.getEQCheckModelByIDs(equipmentIds);

            //过滤去掉activeTIme为空的值
            if (!CollectionUtils.isEmpty(equipmentCheckModels)) {
                equipmentCheckModels = equipmentCheckModels
                        .parallelStream()
                        .filter(e -> e.getActiveTime() != null)
                        .collect(Collectors.toList());
                //得到查询条件集合List<EquipmentModel>
                equipmentCheckModels.forEach(e->activeStatusDtos.add(new ActiveStatusDto(e.getActiveTime(),e.getEquipmentId())));
            }

            //查询得到开始时间大于激活时间的数据
            List<EquipmentStatsModel> eqCheckModelS = eqCheckModelDao.getEQCheckModelByIDAndTime(activeStatusDtos);

            if (!CollectionUtils.isEmpty(eqCheckModelS)) {
                eqCheckModelS.parallelStream()
                        .forEach(e -> e.setTaskId(task.getId()));
                eqCheckModelS.parallelStream()
                        .forEach(e -> e.setId(null));
            }
            //插入
            if(!CollectionUtils.isEmpty(eqCheckModelS)){
                eqCheckModelDao.insertBatch(eqCheckModelS, "equipmentStatsTask");
            }

            Task nextTask = task.getNextTask();
            Map<String, Object> params = nextTask.getParams();
            String url  = dataUrl + "/public/equipment/stats/task/equipmentStatsBatch?page=1&pageNum=10&mapType=bd09&taskId=" + task.getId();
            params.put("dataUrl",url);
            nextTask.setParams(params);
            nextTask.setId(task.getId());
            taskManger.distributeTask(nextTask);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

